﻿using System;
using System.Diagnostics;

namespace MikeKappel.Com.CS
{
    public static partial class SortingAlgorithms
    {
        public delegate Boolean GnomeDelegate(Int32 currentIndex, Int32 currentValue);

        public static void GnomeSort(GnomeDelegate onSort, Int32[] items)
        {
            for (Int32 i = 1, tempvalue; i < items.Length; )
            {
                if (onSort != null && !onSort(i, items[i]))
                    goto escape;

                if (items[i - 1] <= items[i])
                    i++;
                else
                {
                    tempvalue = items[i - 1];
                    items[i - 1] = items[i];
                    items[i] = tempvalue;
                    i--;
                    if (i == 0)
                        i = 1;
                }
            }

        escape:
            return;

        }
        public static void GnomeSort(Int32[] items)
        {
            GnomeSort(null, items);
        }
    }
}
